<?php

class records_center_widget
{
    private $dir = '';

    public function __construct() {
        $this->dir = EXPORT_PATH;
        $this->tablename = 'metadata_dealer_order_goods_sku_daily';
        $this->data_model = array_fill_keys(array(
            'order_id',
            'order_type',
            'store_id',
            'user_id',
            'agency_id',
            'province_code',
            'city_code',
            'county_code',
            'shipping_type',
            'category_id',
            'goods_id',
            'sku_id',
            'cost',
            'goods_price',
            'goods_num',
            'real_price',
            'diff_sum_price',
            'date',
            'add_time',
        ), null);

        $this->goods_category_index = goods_config::get_goods_category_ids();
    }

    public function daily_summary_agency_goods_sku_once() {
        $oldest = _model('order')->read(array('deliver_time !=' => '0000-00-00 00:00:00'), ' ORDER BY `deliver_time` ASC LIMIT 1');
        $oldest_time = strtotime($oldest['deliver_time']) - 86400;
        $last_time = strtotime('-1 day');

        while ($oldest_time < $last_time) {
            $this->daily_summary_agency_goods_sku($oldest_time);
            $oldest_time += 86400;
            ob_flush();
        }
    }

    /**
     * 存入数据表
     */
    public function daily_summary_agency_goods_sku($date = null) {
        if (!$date) {
            $date = strtotime('-1 day');
        }

        $date = date('Y-m-d', $date);

        echo "\n" . '>>> START ' . $date . ' ===============' . "\n";

        //        $sum = _model($this->tablename)->getTotal(array('order_type' => 1, 'date' => $date));
        //
        //        if ($sum) {
        //            echo 'EXISTS    ::' . $date . "\n";
        //            return;
        //        }

        $shipping_type = 0;

        $start_time = $date . ' 00:00:00';
        $end_time = $date . ' 23:59:59';

        $order_list_confirm = array_to_filelds_assoc('id', _model('order')->field('id,type,store_id,user_id')->getList(enable_filter(array(
            'is_enable' => 1,
            'type' => 1,
            'progress != ' => order_config::DEALER_CANCEL,
            'confirm_order_time >= ' => $start_time,
            'confirm_order_time <= ' => $end_time,
        ))));

        $order_list_deliver = array_to_filelds_assoc('id', _model('order')->field('id,type,store_id,user_id')->getList(enable_filter(array(
            'is_enable' => 1,
            'type' => 1,
            'progress != ' => order_config::DEALER_CANCEL,
            'deliver_time >= ' => $start_time,
            'deliver_time <= ' => $end_time,
            'confirm_order_time' => '0000-00-00 00:00:00',
        ))));

        $order_list = $order_list_confirm + $order_list_deliver;

        echo ' - confirms (' .
             count($order_list_confirm) .
             ') - delivers (' .
             count($order_list_deliver) .
             ') - orders (' .
             count($order_list) .
             ')' .
             "\n";

        if (empty($order_list)) {
            echo ' - NO RECORD :: ' . $date . "\n";
            echo '<<< END ' . "\n";
            return;
        }

        // 检测是否已经存在
        $decide_order_ids = run_sql($this->tablename, 'getAll', 'SELECT `order_id` FROM `' .
                                                                $this->tablename .
                                                                '` WHERE `order_id` IN(' .
                                                                join(',', array_keys($order_list)) .
                                                                ') AND `shipping_type`=' .
                                                                $shipping_type .
                                                                ' GROUP BY `order_id`');

        if (count($decide_order_ids)) {
            $decide_order_ids = array_to_filelds_assoc('order_id', $decide_order_ids);
            $order_list = array_diff_key($order_list, $decide_order_ids);
            echo ' - exists (' . count($decide_order_ids) . ') - remaining (' . count($order_list) . ')' . "\n";
        }

        if (empty($order_list)) {
            echo ' - ALL HAVE BEEN EXISTS :: ' . $date . "\n";
            echo '<<< END ' . "\n";
            return;
        }

        foreach ($order_list as $order) {
            $location = dealers_helper::get_dealers_location($order['user_id'])[$order['user_id']];

            $order['order_id'] = $order['id'];
            $order['agency_id'] = $order['user_id'];

            // 购买
            $detail = _model('order_relation')->field('goods_id,sku_id,goods_price,goods_num,real_price,diff_sum_price')->getList(array('order_id' => $order['id']));

            foreach ($detail as $goods) {
                $data = parse_param($this->data_model, $order, $goods, array(
                    'order_type' => 1,
                    'cost' => 0,
                    'shipping_type' => $shipping_type,
                    'category_id' => $this->goods_category_index[$goods['goods_id']],
                    'province_code' => $location[0],
                    'city_code' => $location[1],
                    'county_code' => $location[2],
                    'date' => $date,
                    'add_time' => datetime(),
                ));

                _model($this->tablename)->create($data);
            }
        }

        echo ' - SUCCESS   ::' . $date . "\n";
        echo '<<< END ' . "\n";
    }
}